#!/usr/bin/python

from datetime import datetime
from datetime import timedelta
import unittest

from hips.scheduling import schedule


class ScheduleTestCase(unittest.TestCase):

  def testFirstDayOfMonthWithDelta(self):
    """Tests the FirstDayOfMonthWithDelta function."""
    dt = datetime(2009, 6, 1)
    new_dt = schedule.FirstDayOfMonthWithDelta(dt, delta_months=1)
    self.assertEquals(datetime(2009, 7, 1), new_dt)

    dt = datetime(2009, 12, 1)
    new_dt = schedule.FirstDayOfMonthWithDelta(dt, delta_months=1)
    self.assertEquals(datetime(2010, 1, 1), new_dt)

  def AssertCreateScheduleForYearAndMonth(self, expected_days, year, month):
    t = datetime(year, month, 1, 6, 0, 0)
    s = schedule.CreateScheduleStartingAtMonth(t)
    self.assertEquals(expected_days, s.days)
    self.assertEquals(expected_days * 2, s.ShiftCount())
    blocks = s.ScheduleBlocks()
    self.assertEquals(expected_days * 2, len(blocks))
    expected_block_start = t
    expected_block_duration = timedelta(hours=12)
    for block in blocks:
      self.assertEquals(expected_block_start, block.start_time)
      self.assertEquals(expected_block_duration, block.duration)
      expected_block_start = expected_block_start + expected_block_duration

  def testCreateScheduleStartingAtMonthJanuary(self):
    self.AssertCreateScheduleForYearAndMonth(31, 2009, 1)

  def testCreateScheduleStartingAtMonthFebruaryNoLeapYear(self):
    self.AssertCreateScheduleForYearAndMonth(28, 2009, 2)

  def testCreateScheduleStartingAtMonthFebruaryLeapYear(self):
    self.AssertCreateScheduleForYearAndMonth(29, 2012, 2)

  def testCreateScheduleStartingAtMonthMarch(self):
    self.AssertCreateScheduleForYearAndMonth(31, 2009, 3)

  def testCreateScheduleStartingAtMonthApril(self):
    self.AssertCreateScheduleForYearAndMonth(30, 2009, 4)

  def testCreateScheduleStartingAtMonthMay(self):
    self.AssertCreateScheduleForYearAndMonth(31, 2009, 5)

  def testCreateScheduleStartingAtMonthJune(self):
    self.AssertCreateScheduleForYearAndMonth(30, 2009, 6)

  def testCreateScheduleStartingAtMonthJuly(self):
    self.AssertCreateScheduleForYearAndMonth(31, 2009, 7)

  def testCreateScheduleStartingAtMonthAugust(self):
    self.AssertCreateScheduleForYearAndMonth(31, 2009, 8)

  def testCreateScheduleStartingAtMonthSeptember(self):
    self.AssertCreateScheduleForYearAndMonth(30, 2009, 9)

  def testCreateScheduleStartingAtMonthOctober(self):
    self.AssertCreateScheduleForYearAndMonth(31, 2009, 10)

  def testCreateScheduleStartingAtMonthNovember(self):
    self.AssertCreateScheduleForYearAndMonth(30, 2009, 11)

  def testCreateScheduleStartingAtMonthDecember(self):
    self.AssertCreateScheduleForYearAndMonth(31, 2009, 12)

  def testDayForShiftNumber(self):
    t = datetime(2009, 6, 1, 6, 0, 0)
    s = schedule.CreateScheduleStartingAtMonth(t)
    self.assertEquals(1, s.DayForShiftNumber(0))
    self.assertEquals(15, s.DayForShiftNumber(29))

  def testIsDay(self):
    t = datetime(2009, 6, 1, 6, 0, 0)
    s = schedule.CreateScheduleStartingAtMonth(t)
    self.assertTrue(s.IsDay(2))
    self.assertFalse(s.IsDay(15))

  def testIsNight(self):
    t = datetime(2009, 6, 1, 6, 0, 0)
    s = schedule.CreateScheduleStartingAtMonth(t)
    self.assertTrue(s.IsNight(3))
    self.assertFalse(s.IsNight(16))


if __name__ == '__main__':
  unittest.main()
